#define _CRT_SECURE_NO_WARNINGS 1
#include <unordered_set>
using namespace std;
  struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };
 
class Solution {
public:
    bool hasCycle(ListNode* head)
    {
        unordered_set<ListNode*> se;
        ListNode* cur = head;
        while (cur)
        {
            if (se.find(cur) != se.end())
            {
                return true;
            }
            se.insert(cur);
            cur = cur->next;
        }
        return false;
    }
};